<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>补偿业务-收入补偿</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/layui/css/layui.min.css">
    <link rel="stylesheet" href="/css/custom.form.css">
    <style>
        .layui-form-item {
            margin-left: 20px;
        }
    </style>
</head>
<body>

<div>
    <form id="searchForm">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <label for="ajbh"></label>
                <input type="text" id="ajbh" class="layui-input" autocomplete="off"
                       placeholder="请输入检查案件编号">
            </div>
            <div class="layui-input-inline">
                <label for="nsrmc"></label>
                <input type="text" id="nsrmc" class="layui-input" autocomplete="off"
                       placeholder="请输入纳税人名称">
            </div>
            <div class="layui-input-inline">
                <label for="nsrsbh"></label>
                <input type="text" id="nsrsbh" class="layui-input" autocomplete="off"
                       placeholder="请输入纳税人识别号">
            </div>
            <div class="layui-input-inline">
                <label for="rkrqq"></label>
                <input type="text" readonly class="layui-input" id="rkrqq" placeholder="入库日期起">
            </div>
            <div class="layui-input-inline">
                <label for="rkrqz"></label>
                <input type="text" readonly class="layui-input" id="rkrqz" placeholder="入库日期止">
            </div>
            <div class="layui-input-inline">
                <label for="rkxhrqq"></label>
                <input type="text" readonly class="layui-input" id="rkxhrqq" placeholder="入库销号日期起">
            </div>
            <div class="layui-input-inline">
                <label for="rkxhrqz"></label>
                <input type="text" readonly class="layui-input" id="rkxhrqz" placeholder="入库销号日期止">
            </div>
            <div class="layui-input-inline" style="width: auto;">
                <button type="button" class="layui-btn" onclick="searchSrbc()" id="search">查询</button>
                <button type="button" class="layui-btn" id="resetBtn">重置</button>
                <button type="button" class="layui-btn" id="export">导出</button>
            </div>
        </div>
        <div class="layui-form-item">
            <button type="button" class="layui-btn" onclick="downloadSrbcData()" id="downloadData">下载导入模板
            </button>
            <button type="button" class="layui-btn" id="import">导入收入补偿</button>
        </div>
    </form>
    <table class="layui-table" id="showTable" lay-filter="showTable"></table>
</div>
<!-- 工具栏定义 -->
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="update">更新</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>

<!-- 如果需要自定义操作按钮的工具条区域 -->
<div id="toolbar"></div>


</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    //获取token
    let token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    let tokenQuery = token.replace("#", "%23");
    let tableIns1;
    let table = layui.table;
    let form = layui.form;
    let layer = layui.layer;
    let $ = jQuery = layui.jquery;
    let laydate = layui.laydate;
    let upload = layui.upload;
    layui.use(['table', 'layer', 'laydate', 'form'], function () {
        //加载table
        tableIns1 = table.render({
            elem: '#showTable'
            , contentType: 'application/json'
            , headers: {"authorization": token}
            , page: true //开启分页
            , url: '/compensate/querySrbc' //数据接口
            , method: 'POST'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
                }
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {width: 180, field: 'ajbh', title: '案件编号', align: 'center', sort: true},
                    {width: 220, field: 'ajmc', title: '案件名称', align: 'center', sort: true, edit: 'text'},
                    {width: 200, field: 'nsrsbh', title: '纳税人识别号', align: 'center', sort: true, edit: 'text'},
                    {width: 320, field: 'nsrmc', title: '纳税人名称', align: 'center', sort: true, edit: 'text'},
                    {width: 180, field: 'zsxm', title: '征收项目', align: 'center', sort: true, edit: 'text'},
                    {width: 180, field: 'zspm', title: '征收品目', align: 'center', sort: true, edit: 'text'},
                    {width: 180, field: 'sjje', title: '实缴金额', align: 'center', sort: true, edit: 'text'},
                    {
                        field: 'rkrq', title: '入库日期', width: 140, align: "center", templet: function (d) {
                            if (!d.rkrq) {
                                return '';
                            }
                            return layui.util.toDateString(d.rkrq, 'yyyy-MM-dd');
                        }
                    },
                    {
                        field: 'rkxhrq', title: '入库销号日期', width: 140, align: "center", templet: function (d) {
                            if (!d.rkxhrq) {
                                return '';
                            }
                            return layui.util.toDateString(d.rkxhrq, 'yyyy-MM-dd');
                        }
                    },
                    {
                        width: 240,
                        field: 'skssswjg',
                        title: '税款所属税务机关',
                        align: 'center',
                        sort: true,
                        edit: 'text'
                    },
                    {width: 140, field: 'skzl', title: '税款种类', align: 'center', sort: true, edit: 'text'},
                    {width: 140, field: 'sksx', title: '税款属性', align: 'center', sort: true, edit: 'text'},
                    {width: 140, field: 'zyj', title: '中央级', align: 'center', sort: true, edit: 'text'},
                    {width: 120, toolbar: "#tool", title: '操作', align: 'center'}
                ]
            ]
            , toolbar: '#toolbar'
        });

        table.on('tool(showTable)', function (obj) {
            var data = obj.data; // 获取当前行数据
            var layEvent = obj.event; // 获取事件类型
            if (layEvent === 'update') { // 更新操作
                // 这里写更新操作的代码，您可以弹出更新表单并填充数据
                console.log('更新操作，数据：', data);
                // 发送修改后的值到后端
                $.ajax({
                    url: '/compensate/updateSrbc',
                    method: 'POST',
                    contentType: 'application/json',
                    headers: {"authorization": token},
                    data: JSON.stringify(data),
                    success: function (res) {
                        if (res.data === true) {
                            searchSrbc();
                            layer.msg('更新成功');
                        } else {
                            layer.msg('更新失败: ' + res.msg);
                        }
                    },
                    error: function () {
                        layer.msg('更新失败，服务器错误');
                    }
                });
            } else if (layEvent === 'delete') { // 删除操作
                // 这里写删除操作的代码
                console.log('删除操作，数据：', data);
                // 例如弹出确认框
                layer.confirm('确定删除该案件吗？', {icon: 3, title: '提示'}, function (index) {
                    // 发起删除请求
                    $.ajax({
                        url: '/compensate/deleteSrbc',
                        method: 'POST',
                        contentType: 'application/json',
                        headers: {"authorization": token},
                        data: JSON.stringify(data),
                        success: function (response) {
                            if (response.data === true) {
                                obj.del(); // 删除表格中的当前行
                                layer.msg('删除成功');
                                searchSrbc();
                            } else {
                                layer.msg('删除失败');
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });

        //导出
        $('#export').on('click', function () {
            const rkrqqValue = $("#rkrqq").val();
            const rkrqzValue = $("#rkrqz").val();
            const rkxhrqqValue = $("#rkxhrqq").val();
            const rkxhrqzValue = $("#rkxhrqz").val();
            layer.confirm('确定导出所有数据?', {icon: 3, title: '提示'}, function (index) {
                const mapArr = {
                    ajbh: $("#ajbh").val(),
                    ajmc: $("#ajmc").val(),
                    nsrsbh: $("#nsrsbh").val(),
                    nsrmc: $("#nsrmc").val(),
                    zsxm: $("#zsxm").val(),
                    zspm: $("#zspm").val(),
                    ajzt: $("#ajzt").val(),
                    rkrqq: rkrqqValue,
                    rkrqz: rkrqzValue,
                    rkxhrqq: rkxhrqqValue,
                    rkxhrqz: rkxhrqzValue,
                    skssswjg: $("#skssswjg").val(),
                    skzl: $("#skzl").val(),
                    sksx: $("#sksx").val(),
                };
                CoreUtil.sendPost("/compensate/exportSrbc", mapArr, function (res) {
                    if (res.code === 0) {
                        const data = res.data;
                        if (!CoreUtil.isEmpty(data)) {
                            //全部导出
                            table.exportFile(tableIns1.config.id, data, 'xls');
                            //导出时阻止页面刷新
                            $(".layui-form").submit(function (e) {
                                e.preventDefault();
                            })
                        } else {
                            layer.alert('未查询到需要导出的数据！', {icon: 2});
                        }
                    }
                });
                layer.close(index);
            });
        });

    });

    //下载模板
    function downloadSrbcData() {
        fetch('/compensate/downloadTemplate', {
            method: 'GET',
            headers: {"authorization": token}
        })
            .then(response => response.blob())
            .then(data => {
                var blob = new Blob([data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
                var link = document.createElement('a');
                link.href = URL.createObjectURL(blob);
                link.download = "收入补偿模板.xlsx"; // 可指定文件名
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            })
            .catch(error => {
                console.error('请求失败:', error);
                alert('文件下载失败，请稍后重试');
            });

    }

    //导入模板
    upload.render({
        elem: '#import', // 绑定按钮
        headers: {"authorization": token},
        url: '/compensate/uploadExcel',  // 上传接口地址
        accept: 'file',       // 允许上传的文件类型
        exts: 'xlsx|xls',     // 允许上传的文件后缀
        done: function (res) {
            if (res.code === 0) {
                layer.msg("导入成功！" + res.msg + "!", {icon: 1});
                searchSrbc();
            } else {
                layer.msg("导入失败！" + res.msg, {icon: 2});
            }
        },
        error: function () {
            layer.msg('上传失败', {icon: 2});
        }
    });

    // 监听重置按钮
    document.getElementById('resetBtn').addEventListener('click', function () {
        document.getElementById('searchForm').reset(); // 原生重置表单
        form.render(); // 重新渲染 Layui 的动态组件
    });

    //执行查询
    function searchSrbc() {
        const rkrqqValue = $("#rkrqq").val();
        const rkrqzValue = $("#rkrqz").val();
        const rkxhrqqValue = $("#rkxhrqq").val();
        const rkxhrqzValue = $("#rkxhrqz").val();
        if (!rkrqqValue && rkrqzValue) {
            layer.msg('请输入入库日期起', {icon: 5, time: 1500});
            return;
        } else if (rkrqqValue && !rkrqzValue) {
            layer.msg('请输入入库日起止', {icon: 5, time: 1500});
            return;
        } else if (rkrqqValue > rkrqzValue) {
            layer.msg('入库日起起不能大于入库日起止', {icon: 5, time: 1500});
            return;
        }
        if (!rkxhrqqValue && rkxhrqzValue) {
            layer.msg('请输入入库销号日期起', {icon: 5, time: 1500});
            return;
        } else if (rkxhrqqValue && !rkxhrqzValue) {
            layer.msg('请输入入库销号日起止', {icon: 5, time: 1500});
            return;
        } else if (rkxhrqqValue > rkxhrqzValue) {
            layer.msg('入库销号日起起不能大于入库销号日起止', {icon: 5, time: 1500});
            return;
        }

        //这里以搜索为例
        tableIns1.reload({
            where: { //设定异步数据接口的额外参数，任意设
                ajbh: $("#ajbh").val(),
                ajmc: $("#ajmc").val(),
                nsrsbh: $("#nsrsbh").val(),
                nsrmc: $("#nsrmc").val(),
                zsxm: $("#zsxm").val(),
                zspm: $("#zspm").val(),
                ajzt: $("#ajzt").val(),
                rkrqq: rkrqqValue,
                rkrqz: rkrqzValue,
                rkxhrqq: rkxhrqqValue,
                rkxhrqz: rkxhrqzValue,
                skssswjg: $("#skssswjg").val(),
                skzl: $("#skzl").val(),
                sksx: $("#sksx").val(),
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    }

    //入库日期起
    laydate.render({
        elem: '#rkrqq'
        , type: 'date'
        , value: null
        , done: function (value) {
            if (value !== null && value !== undefined && value !== "") {
                startTime = value;
            } else {
                startTime = null;
            }
        }
    });
    //入库日期止
    laydate.render({
        elem: '#rkrqz'
        , type: 'date'
        , value: null
        , done: function (value) {
            if (value !== null && value !== undefined && value !== "") {
                endTime = value;
            } else {
                endTime = null;
            }
        }
    });
    //入库日期起
    laydate.render({
        elem: '#rkxhrqq'
        , type: 'date'
        , value: null
        , done: function (value) {
            if (value !== null && value !== undefined && value !== "") {
                startTime = value;
            } else {
                startTime = null;
            }
        }
    });
    //入库日期起
    laydate.render({
        elem: '#rkxhrqz'
        , type: 'date'
        , value: null
        , done: function (value) {
            if (value !== null && value !== undefined && value !== "") {
                startTime = value;
            } else {
                startTime = null;
            }
        }
    });
</script>